package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {

    @Autowired
    private  CourseService courseService;


    /**
     * 获取课程列表
     */
    @GetMapping
    public Result list(String name, Integer subject, Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询课程: name={}, subject={},target={}, page={}, pageSize={}", name, subject, target,page, pageSize);
        //调用Service，分页查询
        PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 添加课程
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加部门: {}", course);
        courseService.add(course);
        return Result.success();
    }

    /**
     * 根据id删除课程
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除课程: {}", id);
        courseService.deleteById(id);
        return Result.success();
    }
    /**
     * 根据id修改课程
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程: {}", course);
        courseService.update(course);
        return Result.success();
    }
    /**
     * 根据id查询课程
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询id：{}",id);
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
    /**
     * 查询所有课程
     */
    @GetMapping("/list")
    public Result selectAll(){
        log.info("查询所有课程");
        List<Course> courseList = courseService.selectAll();
        return Result.success(courseList);
    }
    /**
     * 根据学科查询课程
     */
    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable Integer subject){
        log.info("查询学科：{}",subject);
        List<Course> courseList = courseService.selectBySubject(subject);
        return Result.success(courseList);
    }
}
